package com.arron.ioAndNio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

/**
 * @Classname TestBlockingIo
 * @Date 2020/8/27 23:43
 * @Created by 刘
 */
public class TestBlockingIo {


    //服务端
    public static void main(String[] args) throws IOException {

        String a= "asa";
        a.hashCode();

        //拿到通道
        ServerSocketChannel open = ServerSocketChannel.open();
        open.bind(new InetSocketAddress(8080));

        //等待连接,获取客户端的通道
        SocketChannel socketChannel = open.accept();

        //读取本地文件
        FileChannel open1 = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ,StandardOpenOption.CREATE);

        ByteBuffer buffer = ByteBuffer.allocate(1024);

        while (open1.read(buffer) != -1){
            buffer.flip();
            socketChannel.write(buffer);
            buffer.clear();
        }

        //关闭通道
        socketChannel.close();
        open1.close();
        open.close();

    }



}
